package com.wiringweb.data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sad
 */
public class Tree<T> {

    private Node<T> root;

    public Tree() {
        root = new Node<>();
    }

    public Node<T> getRoot() {
        return root;
    }

    public void Root(Node<T> root) {
        this.root = root;
    }

    public static class Node<T> {

        private Node<T> parent;
        private final List<Node<T>> childs;
        private T data;

        public Node() {
            this.childs = new ArrayList<>();
        }

        public Node(T data) {
            this.childs = new ArrayList<>();
            this.data = data;
        }

        public Node<T> getParent() {
            return parent;
        }

        public void setParent(Node<T> parent) {
            this.parent = parent;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        public void addChild(Node<T> node) {
            node.setParent(this);
            childs.add(node);
        }

        public List<Node<T>> getChildren() {
            return childs;
        }

        @Override
        public String toString() {
            return data==null?"null":data.toString();
        }
    }
}
